package com.swedne.test.aes;


import android.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import java.io.UnsupportedEncodingException;
import java.security.Key;

/**
 * @version V1.0
 * @desc AES 加密工具类
 */
public class AESUtil {

    private static final String AESTYPE = "AES/ECB/NoPadding";
    //    private static final String AESTYPE_256 = "AES/ECB/PKCS7Padding";
    private static final String AESTYPE_256 = "AES/ECB/PKCS5Padding";

//    public static String getBase64(String str) {
//        byte[] b = null;
//        String s = null;
//        try {
//            b = str.getBytes("utf-8");
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
//        if (b != null) {
//            s = new BASE64Encoder().encode(b);
//        }
//        return s;
//    }
//
//    // 解密
//    public static String getFromBase64(String s) {
//        byte[] b = null;
//        String result = null;
//        if (s != null) {
//            BASE64Decoder decoder = new BASE64Decoder();
//            try {
//                b = decoder.decodeBuffer(s);
//                result = new String(b, "utf-8");
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//        return result;
//    }

    public static String AES_Encrypt(String plainText) {
        String antKey = "atsmartlife12345";
        return AES_Encrypt(antKey, plainText);
    }

    public static String AES_Encrypt_Lg(String plainText) {
        String antKey = "C8j5rDJXvuoj3LtBMuN7MeWykKcgXwTzNn0jqSU20qbUXBAfhQv2IwoYXko8rPnY";
        return AES_Encrypt_Lg(antKey, plainText);
    }


    //AES加密
    public static String AES_Encrypt_Lg(String keyStr, String plainText) {
        byte[] encrypt = null;
        try {
//            SecretKeySpec keySpec = new SecretKeySpec(keyStr.getBytes(), "AES");

            Key key = generateKey(keyStr);
//            Cipher cipher = Cipher.getInstance("AES");
            Cipher cipher = Cipher.getInstance(AESTYPE_256);
            cipher.init(Cipher.ENCRYPT_MODE, key);
//            int pad = plainText.getBytes().length % 16;
//            if (pad != 0) {
//                for (int i = 0; i < 16 - pad; i++) {
//                    plainText = plainText + "\0";
//                }
//            }
//            byte c[] = plainText.getBytes();
            encrypt = cipher.doFinal(plainText.getBytes("utf-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Base64.encodeToString(encrypt, Base64.DEFAULT);
    }

    public static String AES_Encrypt(long plainText) {
        String antKey = "atsmartlife12345";
        return AES_Encrypt(antKey, plainText + "");
    }

    public static String AES_Encrypt(String keyStr, String plainText) {
        byte[] encrypt = null;
        try {
            Key key = generateKey(keyStr);
            Cipher cipher = Cipher.getInstance(AESTYPE);
            cipher.init(Cipher.ENCRYPT_MODE, key);
            int pad = plainText.getBytes().length % 16;
            if (pad != 0) {
                for (int i = 0; i < 16 - pad; i++) {
                    plainText = plainText + "\0";
                }
            }
            byte c[] = plainText.getBytes();
            encrypt = cipher.doFinal(plainText.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new String(Base64.encodeToString(encrypt, Base64.NO_PADDING));
    }


    //AES解密
    public static String AES_Decrypt(String keyStr, String encryptData) {
        byte[] decrypt = null;
        try {
            Key key = generateKey(keyStr);
            Cipher cipher = Cipher.getInstance(AESTYPE);
            cipher.init(Cipher.DECRYPT_MODE, key);
            decrypt = cipher.doFinal(Base64.decode(encryptData, Base64.DEFAULT));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new String(decrypt).trim();
    }


    private static Key generateKey(String key) throws Exception {
        try {
            SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
            return keySpec;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }

    }

    public static String getKeyFromSip(String sip) {
        sip = sip.replace("_", "");
        if (sip.length() >= 16) {
            sip = sip.substring(sip.length() - 16);
        } else {
            for (int i = sip.length(); i < 16; i++) {
                sip = '0' + sip;
            }
        }
        return sip;

    }
//
//    public static void main(String[] args) {
//        String recv = AES_Decrypt("atsmartlife12345",
//                "bZ3UubtHcIrHJHaNnLh1UGTrEC7TNhpIhR4MktMBl8DffxMMzVL5pQ3ayAlTMDGHVgOU17oBznxzlxW+UhtZ0Q==");
//        System.out.println(recv);
//    String keyStr1 = "18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1";
    //List<Integer> list = Lists.newArrayList(18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1);

//        Integer personCode = 1000022;
//        String sceneId = "30feb11be91f43beacc89cdf7ab2b834";
//        String sceneName = "回家模式";
//        Integer updateType = 1;

    //  String keyStr1 = "{\"custId\":\"53\"}";
//        String keyStr1 = "1000022";findAppointmentTime
//        String decString = AES_Encrypt("atsmartlife12345", keyStr1);
//        System.out.println("decString:" + decString);

//        String e = "";
//        for (int t = 0; t < 4; t++) {
//            Double temp = Math.floor(2821109907456L * Math.random());
//            String temp1 = "0000000".concat(Integer.toString(temp.intValue(), 36));
//            temp1 = temp1.substring(temp1.length() - 8, temp1.length());
//            e += temp1;
//        }
//        System.out.println(e);
//        for (var e = "", t = 0; t < 4; t++)
//            e += "0000000".concat(Math.floor(2821109907456L * Math.random()).toString(36)).slice(-8);
//        return e

//    }

//    public static void main(String[] args) {
//        JSONObject jsonObject =new JSONObject();
//        String antkey = "atsmartlife12345";
//        jsonObject.put("projectCode",56);
//        jsonObject.put("customerCode",10001);
//        jsonObject.put("villageCode",10001);
//        jsonObject.put("appointmentDay","2021-01-21");
//        String a ="ZNMrJ15Q6TulzqAYyn9qeeakcEz6k0vfW2vQ3QuSLv/2EW8Qu6vfvRuKqaOShZCLldf0+mM6/Qoj3J6wCiunLS2X8EcQZUffEPfW56phMIraP3x+sNDUXGHc+GnHFV3EwnRQYnSbcoVC+YdGFS3jL2xdsnDeoKJtu/TCQfM6rWpHpjJ/DraMHnMwFcyCZAlSDtZTWyzAsz9W+RPT2LUq/KSGkHFp/VCbRbBGBAPlRJM2ATxGXJ4S9nvFvD7ucEmx14+doCjFJnyymfl11p8bB8ILgi1mcq/lv+/vW6MYlbEtWko41Z7EZKbEJZE/XGkxyrvc6ubqdwPJuDmhgEEVQW+i/P20k/LqOcHjgptxLLGuq78Oi1gok9Pq9Z+YRpvJOHwxDlPTPgjgun6p7bPLmySphpTyWe+McTGgWLrZ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32OlcLZTda8R8=";
//        String encryptData1 = AES_Encrypt( antkey,"2021-02-20");
//        String encryptData0 = AES_Encrypt( antkey,"[\"16:00~16:30\",\"16:30~17:00\"]");
//        String encryptData2= AES_Encrypt( antkey,"1");
//        String encryptData3 = AES_Encrypt( antkey,"15617934293");
//        String encryptData4 = AES_Encrypt( antkey,"[56]");
//        String de = AES_Decrypt(antkey,a);
//
//    System.out.println(de);
//        System.out.println(encryptData4);
//    }

    public static void main(String[] args) {
//        String antkey = "atsmartlife12345";
//        String encryptData1 = AES_Encrypt(antkey, "108");
//        String encryptData2 = AES_Encrypt(antkey, "10008");
//        String encryptData3 = AES_Encrypt(antkey, "101");
//        String encryptData4 = AES_Encrypt(antkey, "s3VjA2eQlZh6yXT/DK8b8g==");
//        String encryptData5 = AES_Encrypt(antkey, "xUuOukAuc7ORc+dIZ5gcuA==");
//        System.out.println("projectCode:" + encryptData1 + " customerCode:" + encryptData2 + " sharedSpaceCode:" + encryptData3 + " appointmentDay:" + encryptData4 + " villageCode:" + encryptData5);

//        String xx = AES_Decrypt(antkey, "V72n14tyORv8INlC/ihQSiqnBKvWtgU4igFELoB8Y8UecdTkwdOb1b/01MZfjt1GkAJZIb1bGmVrG/2Fr2cQiAnsE3u3V9xsxD0T+wpqQRE/95/kQ4enmqFrufw79EYPgLUU6y2bfVq65qsxT37+Ngr3ytufgYoTdfnXoX3FtAQA9x/cfUAWqTJHQFo50pVnpCFLCOAsw+brR1M/u3D/UTgBaLCDfrOSi+aCUzdhjtsf9UraX7NWbWnAc3n4TxgN+GgFJxbt9Vt4fmSHlw1baaelApzCyAT2gTylcOT7rKLsDkE8iKjSR6arcaMEhMNiXd5TWnvB6Mao+g2Q45MChHRUtYe/uLp35OHLTREjxKcLkinLVOHW5hYTGH84XNXgAXQukZ59ZE+LlnC4KswFv1Mn0JrO61ayK8R7cjVNhTft2TN8o+K9Ay505r1K9WnEq4uenG2l/50YYUYjxCVWyLfLj52e+4Q08tIcxyYsL51CWSHU9CrsZ48utkYylxUeGylhKmEBA6rAKD/EwyYZcjrw3JS7+8JPAgmUvzkURIJBwtRMuUUnVF3ogPKEVKJUzsu5bbXbGkgO7jrBlHUmDFtzwxAsr+1YwJQjotpZHUG/V5ibhMvqHqn0HfoD0VpOA1p+bp2B12iM5uwcl5pXAM7w5GNqo4esx2rOgEUBDps1ZCTlJzP1dT8m+KQpHIiliq85PxKo06aXL4H3NBcvZhcJPnghin5PoUm97/J1yIK9/tQsm83b5f40yfePfQvj1IZym5iaon2y2kfRz0rQDOKvRoeYR2BSHr/DV3zxZ8zfDVOLJjMsPZAhUAtycnyMYKJ9YzUE+LaZ4D2bdrk8yFQTWY/AYrwxMLEpXlUF5P4d5U9DCzUW2e4n5/SStUsvYgVaLLUJUb1KX38RAwjnosBTmIrNXl3sptlLCVO14iGiz7JnIXU7FSNMxnC9benQDS16HGzwgEnqAiyIUjHGaoyy+ovUzJXXz+NilkA5zwO4xnlUJRo0mgsxIwX6dJun77A3xKwz+VD6l9u7LWWeHN3jgwolV2HTq6JtaMjngV32fE+enEwwg8YycsJp+NsDp03ILzOZFtw9hk4oDCluZ677jKWwe0fVj7YfeX3rXLJ/jPX5wJNdIqjWFglHG8U6Og/RPKP/XmW3nO0pF+u8nKL1+pzYTJtoN8vcbDIhCDyKviuURVROjWcHidB/9brrBzOf9TokqAozZ0wcOzV3hA==");
//        String xx = AES_Encrypt_Lg("123456");
        String str = "*880612341564645;";
        str = str.substring(0, str.length() - 1);
        char c = str.charAt(0);
        System.out.println(c);
        System.out.println(str.length());
        System.out.println(str.getBytes().length);
        System.out.println(str);
    }

//    {"projectCode":"LId13HdaP5DvYrHTERBAW/d9XjhsYXMFnUnVyTzu3wQ=","customerCode":"ViYH+T58P2LWKGxQhLiv5A==","sharedSpaceCode":"jjvR9fOfkZInGCucAnRlSA==","appointmentDay":"s3VjA2eQlZh6yXT/DK8b8g==","villageCode":"xUuOukAuc7ORc+dIZ5gcuA=="}

    private boolean test() {
        return false;
    }
}
